package guoanmaker.operator.business.service;

import guoanmaker.operator.business.model.*;
import guoanmaker.operator.business.modelnobind.ProvideCommodityMove;
import guoanmaker.operator.business.modelnobind.ProvideCommodityServiceMove;
import guoanmaker.operator.business.repository.*;
import guoanmaker.operator.helper.common.util.JsonUtil;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *  
 *
 * @ClassName: Operator2AppService 
 * @Description: Operator2AppService 
 * @author: huZhaoyu 
 * @date 2017/6/28 下午2:56 
 */
@Service
public class Operator2AppService implements Operator2AppServiceInterface {
    private final OperatorActivityTypeRepository operatorActivityTypeRepository;
    private final OperatorHotServerRepository operatorHotServerRepository;
    private final OperatorAppBannerRepository operatorAppBannerRepository;
    private final OperatorHeadlineRepository operatorHeadlineRepository;
    private final OperatorRoundPhotoAppRepository operatorRoundPhotoAppRepository;
    private final OperatorServerClassRepository operatorServerClassRepository;
    @Autowired
	private OperatorBoutiqueRecommendRepository operatorBoutiqueRecommendRepository;
    @Autowired
    private  OperatorHotwordsRepository operatorHotwordsRepository;
    @Autowired
	private OperatorRoomRepository operatorRoomRepository;
	@PersistenceContext
	private EntityManager manager;

    public Operator2AppService(OperatorActivityTypeRepository operatorActivityTypeRepository, OperatorHotServerRepository operatorHotServerRepository, OperatorAppBannerRepository operatorAppBannerRepository, OperatorHeadlineRepository operatorHeadlineRepository, OperatorRoundPhotoAppRepository operatorRoundPhotoAppRepository
                                , OperatorServerClassRepository operatorServerClassRepository) {
        this.operatorActivityTypeRepository = operatorActivityTypeRepository;
        this.operatorHotServerRepository = operatorHotServerRepository;
        this.operatorAppBannerRepository = operatorAppBannerRepository;
        this.operatorHeadlineRepository = operatorHeadlineRepository;
        this.operatorRoundPhotoAppRepository = operatorRoundPhotoAppRepository;
        this.operatorServerClassRepository = operatorServerClassRepository;
    }

    @Override
    public  HashMap<String, Object> getAll2App(String cityId) {
        HashMap<String, Object> reMap = new HashMap<>();
        try{
        long ban = operatorAppBannerRepository.count();
        List<OperatorAppBanner> banners = operatorAppBannerRepository.findBycityCode(cityId);
//        ArrayList<OperatorAppBanner> banners = new ArrayList(Arrays.asList(alls));
        if(banners.size()!=0){
        for (OperatorAppBanner  banner : banners){
        	if(banner.getBanType().equals("1")){
        		if(null!=banner.getItemLevel()&&!banner.getItemLevel().equals("")){
        		if(banner.getItemLevel()==1){
        			if(null!=banner.getServerFirstId()&&!banner.getServerFirstId().equals("")){
        			OperatorServerClass operatorServerClassOne = operatorServerClassRepository.findOne(banner.getServerFirstId());
        			banner.setServerName(operatorServerClassOne.getServerName());
        			if(null!=operatorServerClassOne.getServerType()){
        	            banner.setServerType(operatorServerClassOne.getServerType());}
        	            if(null!=operatorServerClassOne.getServerPicUrl()){
        	            	banner.setIconPath(operatorServerClassOne.getServerPicUrl());
        	            }
        			}
        		}else if(banner.getItemLevel()==2){
        			if(null!=banner.getServerId()&&!banner.getServerId().equals("")){
        			OperatorServerClass operatorServerClassTwo = operatorServerClassRepository.findOne(banner.getServerId());
        			banner.setServerName(operatorServerClassTwo.getServerName());
        			if(null!=operatorServerClassTwo.getServerType()){
        	            banner.setServerType(operatorServerClassTwo.getServerType());}
        	            if(null!=operatorServerClassTwo.getServerPicUrl()){
        	            	banner.setIconPath(operatorServerClassTwo.getServerPicUrl());
        	            }
        		}
        		}
        		}
        	}
        	}
        }
        reMap.put("banners", banners);
        List<OperatorHotServer> servers = operatorHotServerRepository.findBycityCode(cityId);//热门服务
        if(servers.size()!=0){
        for (OperatorHotServer operatorHotServer : servers) {
            OperatorServerClass serverClass = operatorServerClassRepository.findOne(operatorHotServer.getServerId());
            if(null!=serverClass.getServerName()){
            operatorHotServer.setServerName(serverClass.getServerName());}
            if(null!=serverClass.getServerType()){
            operatorHotServer.setServerType(serverClass.getServerType());}
        }
        }
        reMap.put("servers", servers);
        List<OperatorHeadline> headlines = operatorHeadlineRepository.findBycityCode(cityId);
        reMap.put("headlines", headlines);
        List<OperatorRoundPhotoApp> apps = operatorRoundPhotoAppRepository.findBycityCode(cityId);
        reMap.put("apps", apps);
        List<OperatorHotwords> hots=operatorHotwordsRepository.findByCityCodetwo(cityId);
        reMap.put("hots", hots);
        OperatorHotwords hot=operatorHotwordsRepository.findByCityCodeOne(cityId);
        reMap.put("hot", hot);
        Pageable pageable = new PageRequest(0,10);
        Page<OperatorBoutiqueRecommend> ofls=operatorBoutiqueRecommendRepository.findByCityCode(cityId,pageable);
        List<OutOperatorBoutiqueRecommend> outobrs=new ArrayList<OutOperatorBoutiqueRecommend>();
        
        for(OperatorBoutiqueRecommend obr:ofls){
        	OutOperatorBoutiqueRecommend outOperatorBoutiqueRecommend=new OutOperatorBoutiqueRecommend();
        	try{
        	if(obr.getProvideCommodityId().contains("S")){
        		ProvideRoom provideRoom=operatorRoomRepository.findByroomCodes(obr.getProvideCommodityId());
        		
				outOperatorBoutiqueRecommend.setCommitName(provideRoom.getRoomName());
				outOperatorBoutiqueRecommend.setCommitid(provideRoom.getRoomCodes());
				outOperatorBoutiqueRecommend.setCommitUrl(provideRoom.getRoomlogo());
				outOperatorBoutiqueRecommend.setSynopsis(provideRoom.getDescribeAsk());
				outOperatorBoutiqueRecommend.setServerType(obr.getServerType());
				
        	}else{
        		
        		String jpql="select new guoanmaker.operator.business.modelnobind.ProvideCommodityMove( "
        				+ "t.commodityName,b.businessName,t.isBargaining,t.startingPrice,d.dictName,t.invoiceMail,t.isTerritoryRestriction"
        				+ ",t.commodityPrice,t.commodityPricePromotion,t.startTime,t.endTime,t.commodity_bigimage,t.description,t.commodityState,t.releaseTime,t.stock"
        				+ ",t.populationRestriction,t.registrationStartTime,t.registrationEndTime,t.synopsis,t.id,t.type,sv.serverName,t.isPromotion,t.clickRate,t.process"
        				+ ",t.promise,t.commodityCase,t.auditOpinion,t.finalPrice,t.introduce,t.commodity_smallimage,b.LicensPhone,t.commodityCode) "
        				+ " from ProvideCommodity t  left  join t.providerBusiness b left join t.serviceType  sv"
        				+ " left join t.measurementUnit d    where t.commodityState=2 and  t.commodityCode=:commodityCode and t.type!=1 ";
        		Integer code=Integer.parseInt(obr.getProvideCommodityId());
        		Query query=manager.createQuery(jpql);//查询商品信息
        		query.setParameter("commodityCode", code);
        		
				List<ProvideCommodityMove> comms= query.getResultList();
        		ProvideCommodityMove commodity=null;
        		if(comms.size()>0){
        			commodity=comms.get(0);
        			String jpql2="select new guoanmaker.operator.business.modelnobind.ProvideCommodityServiceMove(s.id,ct.code,a.name,ct.name,p.name,ct.provincecode,s.isPromotion,s.commodityPrice,s.commodityPricePromotion,s.startTime,s.endTime,s.finalPrice)"
        					+ " from ProvideCommodityServiceRange s left join  s.commodity c left join s.city ct left join s.area a left join s.province p  where  c.commodityCode=:commodityCode and ct.code=:code order by s.finalPrice";
        			Query query2=manager.createQuery(jpql2);//查询商品提供的服务区域
        			query2.setParameter("commodityCode", code);
        			query2.setParameter("code", cityId);
        			List<ProvideCommodityServiceMove> lists=query2.getResultList();
        			commodity.setServiceRange(lists);
        			if(lists.size()>0){
        				commodity.setFinalPrice(lists.get(0).getFinalPrice());
        			}
        		}
        		outOperatorBoutiqueRecommend.setCommitFinnalPrice(commodity.getFinalPrice());
				outOperatorBoutiqueRecommend.setCommitName(commodity.getCommodityName());
				outOperatorBoutiqueRecommend.setCommitid(commodity.getCommodityCode()+"");
				outOperatorBoutiqueRecommend.setCommitUrl(commodity.getCommodity_smallimage());
				outOperatorBoutiqueRecommend.setSynopsis(commodity.getSynopsis());
				outOperatorBoutiqueRecommend.setServerType(obr.getServerType());
        	
				}
        	
        	outobrs.add(outOperatorBoutiqueRecommend);
        }catch(Exception e){
        	continue;
        }
		}
        
        reMap.put("ofls", outobrs);
        
        }catch(Exception e){
        	e.printStackTrace();
        	System.out.println("精品推荐出现错误");
        }
        return reMap;
    }

}
